package server_logic;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import client_controllers.ProductController;
import ocsf.server.ConnectionToClient;
import ocsf.server.ObservableServer;
import server_gui.ServerLog;
import entity.AddressingCustomer;
import entity.Boss;
import entity.CampaignPattern;
import entity.Customer;
import entity.Employee;
import entity.MarketingCampaign;
import entity.Product;
import entity.Type;


public class inetCon extends ObservableServer{

	ArrayList<String> stc;
	Date lastASupdate;
	public inetCon(int port) {
		super(port);
		// TODO Auto-generated constructor stub
		stc = new ArrayList<String>();
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		lastASupdate=new Date();
		Calendar cl=Calendar.getInstance();
		df.format(lastASupdate.getTime());
		
		
		
	}

	
	  protected void serverStarted()
	  {
	    System.out.println
	      ("Server listening for connections on port " + getPort());
	  }
	  
	  /**
	   * This method overrides the one in the superclass.  Called
	   * when the server stops listening for connections.
	   */
	  protected void serverStopped()
	  {
	    System.out.println
	      ("Server has stopped listening for connections.");
	  }

	 

	  protected void clientConnected(ConnectionToClient client) {
		  ServerLog.log.addElement(new Date().toString()+"Client " +client.getInetAddress() + " connected");
		//  System.out.println("Client " +client + " connected");
	  }
	  
	  
	  synchronized protected void clientDisconnected(
			    ConnectionToClient client) {
		  ServerLog.log.addElement(new Date().toString()+"Client " +client.getInetAddress() + " disconnected");
		//  System.out.println("Client " + client.getName()+" disconnected");
	  }
	  
	  /**
	   * This method handles any messages received from the client.
	   *
	   * @param msg The message received from the client.
	   * @param client The connection from which the message originated.
	   */
	  @SuppressWarnings("unchecked")
	public void handleMessageFromClient(Object msg, ConnectionToClient client)
	  {
		  	System.out.println("handle msg from client");
		  	Boss b=(Boss) msg;
		  	if(b instanceof Boss){
		  			
				  	Commands c=new Commands();
				  	Boss ans;
					if(b.getObj() instanceof Employee){
						Employee emp=(Employee) ((Boss) msg).getObj();
							if(b.getControl().equals("EmployeeLoginController")) {
								switch(b.getCommand()){
									
									case ("search") : //came from login 
											ans=c.Search(b);
											try {
												client.sendToClient(ans);
											} catch (IOException e) {
												
												e.printStackTrace();
											}
										break;
							}
						}
							else if(b.getCommand().equals("logout")){
								ans=c.logout(b);
								try {
									client.sendToClient(ans);
								} catch (IOException e) {
									
									e.printStackTrace();
								}
							}	
							else if(b.getCommand().equals("getCustomer")){
								ans=c.getCustomer(b);
								try {
									client.sendToClient(ans);
								} catch (IOException e) {
									
									e.printStackTrace();
								}
							}
				}//employee
					else if (b.getObj() instanceof Customer){
						Customer customer= (Customer) b.getObj();
						switch(b.getCommand()){
						
						case("add"):
							ans=c.add(b);
							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							break;
						case("getCombo"):
						ans=c.getCombo(b);

						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						break;
						}

					}//customer

					else if (b.getCommand().equals("getSalesManCombo")){
						ans=c.getCustomer(b);
						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						}
					
					else if (b.getCommand().equals("getcategorycombo")){
							ans=c.getCategoryCombo(b);
							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
						}}
					
					else if (b.getCommand().equals("addCategoryToTable")){
							ans=c.addCategoryToTable(b);
							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
						}}
					

					else if (b.getObj() instanceof CampaignPattern){
						CampaignPattern cp= (CampaignPattern) b.getObj();
						switch(b.getCommand()){
							case("getArea"):
								ans=c.getArea(b);
		
								try {
									client.sendToClient(ans);
								} catch (IOException e) {
									e.printStackTrace();
								}							
								break;
							
							case("getProduct"):
								ans=c.getProduct(b);	
								try {
									client.sendToClient(ans);
								} catch (IOException e) {
									e.printStackTrace();
								}
								
								break;
							
							case("new pattern"):
								ans=c.newPattern(b);
							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								e.printStackTrace();
							}
								}
					} //campaign pattern 
					
					else if (b.getObj() instanceof AddressingCustomer){
						AddressingCustomer ac= (AddressingCustomer) b.getObj();
						switch(b.getCommand()){
						
						case("getCampaign"):
							ans=c.getCampaign(b);
							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							break;
						case("get saled product"):
							ans=c.getSaledProduct(b);
							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							break;
						case("getFuturePermission"):
						ans=c.getFuturePermission(b);
						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						case("addpermission"):
							ans=c.addpermission(b);
							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						break;
						case("New Sale or Deny"):
							ans=c.NewSaleOrDeny(b);
							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							break;
						case("combine"):
							ans=c.Combine(b);
							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							break;
							
							
					}}
						else if (b.getObj() instanceof Product){
							
							Product pc= (Product) b.getObj();
							switch(b.getCommand()){
							
							case("getProductNames"):
								
								ans=c.getProductNames(b);
								try {
									client.sendToClient(ans);
								} catch (IOException e) {
									e.printStackTrace();
								}
								break;
							case("deleteProduct"):
								ans=c.deleteProduct(b);
								try {
									client.sendToClient(ans);
								} catch (IOException e) {
									e.printStackTrace();
									
									}
								break;
							case("addProduct"):
								ans=c.addProduct(b);
								try {
									client.sendToClient(ans);
								} catch (IOException e) {
									e.printStackTrace();
									
									}
								break;
						}
						}
					
					else if (b.getCommand().equals("analyze")){
							ans=c.analyze(b);
							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						
					else if (b.getCommand().equals("getCustomerReports")){
						ans=c.getCustomerReports(b);
						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					}
				}
					else if (b.getCommand().equals("getCampaignReports")){
						ans=c.getCampaignReports(b);
						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					}
				}
					else if (b.getCommand().equals("getCampaigns")){
						ans=c.getCampaigns(b);
						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					}
				}




					else if (b.getCommand().equals("getTypes")){
						
							ans=c.getTypes(b);

							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
						}}

					else if (b.getCommand().equals("gettypeCombo")){
						ans=c.gettypeCombo(b);
						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					}}
					
					else if (b.getCommand().equals("addarea")){
							ans=c.addarea(b);
							try {
								client.sendToClient(ans);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
						}}
					else if (b.getCommand().equals("deletearea")){
						ans=c.deletearea(b);
						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					}}
					else if (b.getCommand().equals("getCombo")){
						ans=c.getCombo(b);
						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					}}
					else if (b.getCommand().equals("getproducts")){
						ans=c.getproductsCombo(b);
						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					}}
					else if (b.getCommand().equals("classify")){
						ans=c.classify(b);
						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					}}
					

					//deleteType
					else if (b.getObj() instanceof Type){
						Type type= (Type) b.getObj();
						switch(b.getCommand()){
							case("deleteType"):
								ans=c.deleteType(b);
								try {
									client.sendToClient(ans);
								} catch (IOException e) {
									e.printStackTrace();
								}							
								break;
								
							case("addType"):
								ans=c.addType(b);
								try {
									client.sendToClient(ans);
									
								} catch (IOException e) {
									e.printStackTrace();
								}							
								break;
								}
						
					}
					else if (b.getCommand().equals("getCampainPatterns")){
						ans=c.getCampainPatterns(b);
						try {
							client.sendToClient(ans);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					}}
					else if (b.getObj() instanceof MarketingCampaign){
						
						switch(b.getCommand()){
							case("initiatePatterns"):
								
								ans=c.initiatePatterns(b);
								try {
									client.sendToClient(ans);
								} catch (IOException e) {
									e.printStackTrace();
								}							
								break;
								
							/*case("addType"):
								ans=c.addType(b);
								try {
									client.sendToClient(ans);
									
								} catch (IOException e) {
									e.printStackTrace();
								}							
								break;*/
								}	
						}

		  	
		  	}}}
	  

